package com.kizitonwose.calendarview.fraction;

import com.kizitonwose.calendarview.MainAbility;
import com.kizitonwose.calendarview.ResourceTable;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;

/**
 * @author LiuQingHua
 * @version 1.0
 * @date 2021/2/26/026 15:51
 */
public abstract class BaseFraction extends Fraction {

    Context mContext;
    private Component content;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        mContext = getFractionAbility().getContext();
        content = scatter.parse(getLayoutResId(), null, false);
        initComponent(content, container, intent);

        return content;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(getStatusBarColor());
        getFractionAbility().findComponentById(ResourceTable.Id_toolBar).setVisibility(Component.HIDE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        getFractionAbility().findComponentById(ResourceTable.Id_toolBar).setVisibility(Component.VISIBLE);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(mContext.getColor(ResourceTable.Color_colorPrimaryDark));
    }



    protected abstract int getLayoutResId();

    protected  int getStatusBarColor(){
       return mContext.getColor(ResourceTable.Color_colorPrimaryDark);
    }

    protected abstract void initComponent(Component content, ComponentContainer container, Intent intent);

    /**
     * @noinspection unchecked
     */
    <T extends Component> T findComponentById(int id) {
        return (T) content.findComponentById(id);
    }

    /**
     * 退出当前Fraction
     */
    protected void backFraction() {
        ((MainAbility)getFractionAbility()).getCurrentAbilitySlice().setCurrentFaction(null);
        getFractionAbility().getFractionManager().popFromStack();
    }
}
